<?php
/**
 * Aero - Xistins Technologies PHP Framework
 * 
 * LICENSE
 * This source file is subject to the New BSD license that is bundled 
 * with this package in the LICENSE.txt. It is also available through 
 * the world-wide-web at this URL: http://dev.xistins.com/license/production
 * If you did not receive a copy of this license and are unable to obtain
 * it through the world-wide-web, please send an email to 
 * license@xistins.com so we can send you a copy immediately.
 * 
 * @category   Aero
 * @package    Aero_Db
 * @subpackage Adapter
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Abstract.php 2 2009-11-03 04:10:03Z csouth@xistins.com $
 */

/** @see Aero_Db_Adapter **/
require_once 'Aero/Db/Adapter.php';

/**
 * Abstract database adapter
 * 
 * @category   Aero
 * @package    Aero_Db
 * @subpackage Adapter
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Abstract.php 2 2009-11-03 04:10:03Z csouth@xistins.com $
 */
abstract class Aero_Db_Adapter_Pdo_Abstract extends Aero_Db_Adapter
{
    /**
     * Default Statment Class
     * @var string
     */
    protected $_defaultStatementClass = 'Aero_Db_Statement_Pdo';

    /**
     * Connect to Database
     * 
     * @throws Aero_Db_Exception on failed Db connection
     * @return void
     */
    protected function _connect()
    {
        $this->_checkPdo();

        try {
            $this->_connection = new PDO($this->_getDsn(), $this->_config['username'], $this->_config['password']);
        } catch(PDOException $e) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw new Aero_Db_Exception($e->getMessage());
        }
    }

    /**
     * Verify PDO is installed on server correctly for connection
     * 
     * @return bool
     */
    protected function _checkPdo()
    {
        // Check for PDO Extension
        if(!extension_loaded('pdo')) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw new Aero_Db_Exception('Unable to find PDO extension on server.');
        }

        // Check for PDO Driver
        if(!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
            Aero_Loader::loadClass('Aero_Db_Exception');
            throw Aero_Db_Exception('Unable to find PDO driver for ' . $this->_pdoType . ' on server.');
        }

        return TRUE;
    }

    /**
     * Get DSN for PDO connection
     * 
     * @return string
     */
    protected function _getDsn()
    {
        // baseline of DSN parts
        $dsn = $this->_config;

        // don't pass the username, password, charset, persistent and driver_options in the DSN
        unset($dsn['username']);
        unset($dsn['password']);
        unset($dsn['options']);
        unset($dsn['charset']);
        unset($dsn['persistent']);
        unset($dsn['driver_options']);

        foreach($dsn as $key => $value) {
            $dsn[$key] = "$key=$value";
        }

        return $this->_pdoType . ':' . implode(';', $dsn);
    }
}